/**
 * 
 */
package com.mycompany.client.dataset;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mycompany.client.async.CalendarService;
import com.mycompany.client.async.CalendarServiceAsync;
import com.mycompany.client.model.CalendarBO;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.calendar.CalendarEvent;  

/**
 * @author coral.ma
 *
 */
public class CalendarDataSource extends DataSource {  

	private Long eventId;  
    private String name;  
    private String description;  
    private Date startDate;  
    private Date endDate;  
    private boolean canEdit=true;  
    private String eventWindowStyle;
    private final CalendarServiceAsync calendarService = GWT.create(CalendarService.class);
    
    public CalendarDataSource() {
        DataSourceSequenceField eventIdField = new DataSourceSequenceField("eventId");  
        eventIdField.setPrimaryKey(true);  
        DataSourceTextField nameField = new DataSourceTextField("name");  
        DataSourceTextField descField = new DataSourceTextField("description");  
        DataSourceDateField startDateField = new DataSourceDateField("startDate");  
        DataSourceDateField endDateField = new DataSourceDateField("endDate");  
        this.setFields(eventIdField, nameField, descField, startDateField, endDateField);  
        this.setClientOnly(true);
//        this.setTestData(CalendarOverlappingData.getRecords());
        calendarService.loadCalendars(null,new AsyncCallback<List<CalendarBO>>() {
			
			public void onSuccess(List<CalendarBO> result) {
				List<CalendarEvent> calendarEvents = new ArrayList<CalendarEvent>();
				for(CalendarBO c : result) {
					calendarEvents.add(new CalendarEvent(c.getEventId(),c.getName(),c.getDescription(),c.getStartDate(),c.getEndDate()));
				}
				setTestData(calendarEvents.toArray(new CalendarEvent[]{}));
			}

			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}
		});
    }
    
//    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
//    	System.out.println("***********addData()");
//    }
//    
//    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
//    	System.out.println("***********updateData()");
//    }
//    
//    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
//    	System.out.println("***********removeData()");
//    }
//    
//    public void removeData(Record record, DSCallback callback, DSRequest requestProperties) {
//    	super.removeData(record, callback, requestProperties);
//    	this.add
//    	System.out.println("***********removeData2()");
//    }
}
